var form, element, layer, table, laytpl;
layui.use(['form', 'layer', 'table', 'element', 'laytpl'], function () {
    element = layui.element; //Tab的切换功能，切换事件监听等，需要依赖element模块
    layer = layui.layer;
    form = layui.form;
    table = layui.table;
    laytpl = layui.laytpl;
});
/**
 * 请求公共方法data
 * @param url
 * @param obj
 */
function requsetAjaxobj(url, obj,msg) {
    layer.confirm(msg, function (index) {
        $.ajax({
            type: 'POST',
            url: url,
            dataType: 'json',
            data: obj,
            success: function (data) {
                if (data.code == 0 && data.wait != 3) {
                   
                    layer.msg(data.msg, { icon: 6, time: 1500 }, function () {
                        layer.closeAll();
                        search();
                    })
                } else {
                    layer.msg(data.msg, { icon: 5, time: 1500 })
                }
            }
        });
    });
}
/**
 * 请求公共方法
 * @param url
 * @param obj
 */
function requsetAjax(url, obj,msg) {
    layer.confirm(msg, function (index) {
        $.ajax({
            type: 'POST',
            url: url,
            dataType: 'json',
            data: {
                id: obj.data.id
            },
            success: function (data) {
                if (data.code == 0 && data.wait != 3) {
                   
                    layer.msg(data.msg, { icon: 6, time: 1500 }, function () {
                        layer.closeAll();
                        search();
                    })
                } else {
                    layer.msg(data.msg, { icon: 5, time: 1500 })
                }
            }
        });
    });
}
/**
 * 删除的公共方法
 * @param url
 * @param obj
 */
function deleted(url, obj) {
    layer.confirm('真的删除行么', function (index) {
        $.ajax({
            type: 'POST',
            url: url,
            dataType: 'json',
            data: {
                id: obj.data.id
            },
            success: function (data) {
                if (data.code == 0 && data.wait != 3) {
                    obj.del();
                    layer.msg(data.msg, { icon: 6, time: 1500 }, function () {
                        layer.closeAll();
                    })
                } else {
                    layer.msg(data.msg, { icon: 5, time: 1500 })
                }
            }
        });
    });
}

/**
 * 批量删除
 * @param url
 * @param ids
 */
function deletedAll(url, ids) {
    layer.confirm('确认删除？', function (index) {
        $.ajax({
            type: 'POST',
            url: url,
            dataType: 'json',
            data: {
                id: ids
            },
            success: function (data) {
                if (data.code == 0 && data.wait != 3) {
                    layer.msg(data.msg, { icon: 6, time: 1500 }, function () {
                        layer.closeAll();
                    });
                    search();
                } else {
                    layer.msg(data.msg, { icon: 5, time: 1500 })
                }
            }
        });
    });
}

/**
 * 跳转添加页面的公共方法
 * @param url
 * @returns {boolean}
 */
function add(url, title) {
    $.get(url, {}, function (str) {
        layer.open({
            title: title,
            type: 1,
            content: str
        });
        form.render(null, "alert");
    });
    return false;
}

/**
 * 添加操作的公共方法
 * @param url
 * @param post
 * @param status
 */
function submitPost(url, post) {
    $.ajax({
        type: 'POST',
        url: url,
        dataType: 'json',
        data: {
            post: post
        },
        success: function (data) {
            if (data.code == 0) {
                layer.msg(data.msg, { icon: 6, time: 1500 }, function () {
                    layer.closeAll();
                    search();
                })
            } else {
                layer.msg(data.msg, { icon: 5, time: 1500 })
            }
        },
        error: function (jqXHR) {
            layer.msg('操作失败', { icon: 5, time: 1500 })
        }
    });
}

/**
 * 跳转编辑页面的公共方法
 * @param url
 * @param title
 * @returns {boolean}
 */
function edit(url, title, w, h) {
    var width = w != null ? w : '500px';
    var height = h != null ? h : '';
    var title = title != null ? title : false;
    var url = url != null ? url : "404.html";
    var loading = layer.load();
    $.get(url, {}, function (str) {
        layer.close(loading);
        if (str.code == 0 && str.wait == 3) {
            layer.msg(str.msg, { icon: 5, time: 1500 })
        } else {
            layer.open({
                area: [width, height],
                title: title,
                type: 1,
                content: str,
                maxmin: true,
                shadeClose: true,
                shade: 0.2
            });
            form.render(null, "alert");
        }
    });
    return false;
}

/**
 * 加密
 *
 * @param data
 * @returns {string}
 */
function crypto_encrypt(data) {
    let text = JSON.stringify(data);
    text = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(text)).toString();
    let key = CryptoJS.enc.Utf8.parse('1234567887654321'); //为了避免补位，直接用16位的秘钥
    let iv = CryptoJS.enc.Utf8.parse('1234567887654321'); //16位初始向量
    let encrypted = CryptoJS.AES.encrypt(text, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    }).toString();
    return CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(encrypted)).toString();
}